$package('upms.menu');
upms.menu = function(){
	var _box = null;
	var _this = {
		addAction:baseUrl+'/menu/add'+'?applicationId='+applicationId,
		editAction:baseUrl+'/menu/edit'+'?applicationId='+applicationId,
		deleteAction:baseUrl+'/menu/delete'+'?applicationId='+applicationId,
		editForm:function(){
			return $("#editForm");
		},
		editWin:function(){
			return $("#edit-win");
		},
		add:function(){			
			if(_this.editForm().form('validate')){
				_this.editForm().attr('action',_this.addAction);
				upms.saveForm(_this.editForm(),function(data){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.loadMemuTrees();
					_this.editWin().dialog('close');
				});
			}
		},
		edit:function(){
			if(_this.editForm().form('validate')){
				_this.editForm().attr('action',_this.editAction);
				upms.saveForm(_this.editForm(),function(data){
					var param = $("#searchForm").serializeObject();
					$("#data-list").datagrid('reload',param);
					_this.loadMemuTrees();
					_this.editWin().dialog('close');
				});
			}
		},
	    deleteData:function(){
			var rows = $('#data-list').datagrid('getChecked');
			if (upms.checkSelect(rows)){
					$.messager.confirm('Confirm','是否删除选中记录?',function(r){  
				    if (r){
				    	var arr = [],idKey = 'menuId'; //主键名称
				    	$.each(rows,function(i,record){
				    		arr.push('menuIds='+record[idKey]);
				    	});
				    	var data = arr.join("&");
				    	upms.deleteForm(_this.deleteAction,data,function(){
							var param = $("#searchForm").serializeObject();
							$("#data-list").datagrid('reload',param);
							_this.loadMemuTrees();
						});
				    }  
				});
			}
		},
		loadParentMenuTree:function(){
			//编辑之前加载上级资源列表
			$("#parentId").combotree({
				url:baseUrl+'/menu/menuTree?applicationId='+applicationId
			});
		},
		loadMemuTrees:function(){
			$('#menuTrees').tree({
		          url:baseUrl+'/menu/menuTree?applicationId='+applicationId,
		          checkbox:false,
		          onClick:function(node){
		        	  if(node.id!=-1){
		        		  $("#searchForm").find("input[name=parentId]").val(node.id);  
		        	  }else{
		        		  $("#searchForm").find("input[name=parentId]").val(""); 
		        	  }
		        	  var param = $("#searchForm").serializeObject();
					  $("#data-list").datagrid('reload',param);
					  $("#data-list").datagrid('unselectAll');
					  $("#data-list").datagrid('uncheckAll');
		          }
		    });
		},
		//设置默认按钮数据
		addDefBtns:function(){
			var defaultBtns= [
				{"menuName":"新增","linkUrl":"add"},
				{"menuName":"编辑","linkUrl":"edit"},
				{"menuName":"删除","linkUrl":"delete"}
			];
			var tbline = $(".tb-line:visible");
			$.each(defaultBtns,function(i,btn){
				var isExists = false;
				if(!isExists){
					_this.addLine(btn);
				}
			});
		},
		addLine: function(data){
			var table = $("#btn-tb");
			var	html = "<tr class='tb-line'>";
			html+=	   "	<td align='center'><span  class='newFlag red'>*</span></td>";
			html+=	   "	<td><input name=\"o_menuName\" class=\"easyui-validatebox text-name\" style=\"width:100%\" data-options=\"required:true\"></td>";
			html+=	   "	<td><input name=\"o_linkUrl\" class=\"easyui-validatebox text-desc\" style=\"width:100%\"  data-options=\"required:true\"></td>";
			html+=	   "	<td align='center'><a class=\"easyui-linkbutton remove-btn\"  iconCls=\"icon-remove\" plain=\"true\"></a>";
			html+=	   "	<input class=\"hidden\" name=\"o_menuId\">";
			html+=	   "	<input class=\"hidden\" name=\"o_deleted\" value=\"0\">";
			html+=	   "	</td>";
			html+=	   "</tr>";
			var line = $(html);
			//版定删除按钮事件
			$(".remove-btn",line).click(function(){
				upms.confirm('Confirm','确定要删除当前记录吗?',function(r){
					if(r){
						_this.delLine(line);
					}
				})
			});
			if(data){
				if(data.id){
					$(".newFlag",line).html(''); //清空新增标记
				}
				$("input[name='o_menuName']",line).val(data.menuName);
				$("input[name='o_linkUrl']",line).val(data.linkUrl);
				if(data.menuId){
					$("input[name='o_menuId']",line).val(data.menuId);
				}
				if(data.deleted){
					$("input[name='o_deleted']",line).val(data.deleted);
				}
			}
			$.parser.parse(line);//解析esayui标签
			table.append(line);
		},
		//删除全部
		delAllLine:function(b){
			if(b){
				$(".tb-line").remove();
			}else{
				$(".tb-line").each(function(i,line){
					_this.delLine($(line));
				});
			}
		},
		//删除单行
		delLine:function(line){
			if(line){
				var btnId = $("input[name='o_menuId']",line).val();
				if(btnId){
					$("input[name='o_deleted']",line).val(1); //设置删除状态
					line.fadeOut();
				}else{
					line.fadeOut("fast",function(){
						 $(this).remove();
					});
				}
			}
		},
		initForm:function(){
			_this.loadMemuTrees();
			//新增操作
			$("#addBtn").click(function(){
				$("#editForm").resetForm();
				_this.delAllLine(true);
				_this.loadParentMenuTree();
				if($("#menuTrees").tree('getSelected')){
					$("#parentId").combotree("setValue",$("#menuTrees").tree('getSelected').id);
				}
				$("#edit-win").dialog({
					buttons:[
						{
							text:'保  存',
							iconCls: 'icon-ok',
							handler:function(){
								_this.add();
							}
						},
						{
							text:'关  闭',
							iconCls: 'icon-cancel',
							handler:function(){
								_this.editWin().dialog('close');
							}
						}
				]});
				_this.editWin().dialog('open');
			});
			//删除操作
			$("#deleteBtn").click(function(){
				_this.deleteData();
			});
			
			//修改操作
			$("#editBtn").click(function(){
				$("#editForm").resetForm();
				//只能对一条数据编辑
				var rows = $('#data-list').datagrid('getChecked');
				if(rows.length > 1){
					upms.alert('警告','请单选记录后，再进行操作.','warning');
					return false;
				}
				var row = $('#data-list').datagrid('getSelected');
				if (row){
					_this.loadParentMenuTree();
					var data ={};
					var idKey = 'menuId'; //主键名称
 					data[idKey] = row.menuId;
					upms.getById(baseUrl+"/menu/getById",data,function(result){
						$("#editForm").form('load',result);
						_this.delAllLine(true);
						$.each(result.childrens,function(i,operateMenu){
							_this.addLine(operateMenu);
						});
						$("#edit-win").dialog({
	   						buttons:[
	   							{
	   								text:'保  存',
	   								iconCls: 'icon-ok',
	   								handler:function(){
	   									_this.edit();
	   								}
	   							},
	   							{
	   								text:'关  闭',
	   								iconCls: 'icon-cancel',
	   								handler:function(){
	   									_this.editWin().dialog('close');
	   								}
	   							}
	   					]});
						_this.editWin().dialog('open');
	   				});
				}else{
					upms.alert('警告','未选中记录.','warning');
				}
			});
			$('#addLine_btn').click(_this.addLine);
			$('#addDefLine_btn').click(_this.addDefBtns);
			$('#delAllLine_btn').click(function(){
				upms.confirm('Confirm','确定删除所有记录吗?',function(r){
					_this.delAllLine(false);
				});
			});
		},
		config:{
  			dataGrid:{
  				title:'菜单列表',
	   			url:'dataList?applicationId='+applicationId,
	   			loadMsg:"处理中，请稍候…",
			    toolbar:"#toolbar",
			    idField:"menuId",
	   			columns:[[
						{field:'menuId',checkbox:true},
						{field:'menuName',title:'菜单名称',width:120,align:'left',sortable:true},
						{field:'linkUrl',title:'菜单URL',width:160,align:'left',sortable:true},
						{field:'permission',title:'权限',width:120,align:'center'},
						{field:'applicationName',title:'应用系统',width:120,align:'center'},
						{field:'serialNum',title:'排序',width:45,align:'center',sortable:true},
						{field:'createTime',title:'创建时间',width:140,align:'center',sortable:true,formatter:upms.dateTimeFormatter},
						{field:'updateTime',title:'修改时间',width:140,align:'center',sortable:true,formatter:upms.dateTimeFormatter}
				]]
			}
		},
		init:function(){
			_this.initForm();
			_box = new MyDataGrid(_this.config); 
			_box.init();
		}
	}
	_this._box = _box;
	return _this;
}();

$(function(){
	upms.menu.init();
});		